/**
 * 路由导航工具
 * 提供统一的页面跳转和路由处理功能
 */

import { NavigateFunction } from "react-router-dom";

export interface NavigationUtils {
  goHome: (userRole?: string) => void;
  goToLogin: () => void;
  goBack: () => void;
  goToProfile: (userRole?: string) => void;
  refresh: () => void;
}

/**
 * 创建导航工具
 */
export const createNavigationUtils = (
  navigate: NavigateFunction
): NavigationUtils => {
  return {
    /**
     * 跳转到首页
     */
    goHome: (userRole?: string) => {
      const homeRoutes = {
        admin: "/admin",
        teacher: "/teacher",
        student: "/student",
      };

      const route = userRole
        ? homeRoutes[userRole as keyof typeof homeRoutes]
        : "/";
      navigate(route || "/login");
    },

    /**
     * 跳转到登录页
     */
    goToLogin: () => {
      navigate("/login", { replace: true });
    },

    /**
     * 返回上一页
     */
    goBack: () => {
      navigate(-1);
    },

    /**
     * 跳转到个人资料页
     */
    goToProfile: (userRole?: string) => {
      const profileRoutes = {
        admin: "/admin/profile",
        teacher: "/teacher/profile",
        student: "/student/profile",
      };

      const route = userRole
        ? profileRoutes[userRole as keyof typeof profileRoutes]
        : "/profile";
      navigate(route);
    },

    /**
     * 刷新页面
     */
    refresh: () => {
      window.location.reload();
    },
  };
};

/**
 * 路由路径常量
 */
export const ROUTES = {
  // 公共路由
  LOGIN: "/login",
  ROOT: "/",

  // 管理员路由
  ADMIN: {
    HOME: "/admin",
    DASHBOARD: "/admin/dashboard",
    PROFILE: "/admin/profile",
    TEACHERS: "/admin/teachers",
    STUDENTS: "/admin/students",
    COURSES: "/admin/courses",
    DISCUSSIONS: "/admin/discussions",
  },

  // 教师路由
  TEACHER: {
    HOME: "/teacher",
    PROFILE: "/teacher/profile",
    COURSES: "/teacher/courses",
    COURSES_ADD: "/teacher/courses/add",
    PREPARATION: "/teacher/preparation",
    CLASS_MANAGEMENT: "/teacher/class-management",
    EXAM_GENERATION: "/teacher/exam-generation",
    ANALYSIS: "/teacher/analysis",
    LEARNING_ANALYSIS: "/teacher/learning-analysis",
    RESOURCES: "/teacher/resources",
    DISCUSSIONS: "/teacher/discussions",
  },

  // 学生路由
  STUDENT: {
    HOME: "/student",
    PROFILE: "/student/profile",
    COURSES: "/student/courses",
    EXAMS: "/student/exams",
    RESOURCES: "/student/resources",
    DISCUSSIONS: "/student/discussions",
  },
} as const;

/**
 * 检查路径是否匹配某个路由模式
 */
export const matchRoute = (
  currentPath: string,
  routePattern: string
): boolean => {
  if (routePattern === currentPath) return true;

  // 支持动态路由匹配，如 /courses/:id
  const routeRegex = new RegExp(
    "^" + routePattern.replace(/:[^/]+/g, "[^/]+") + "$"
  );

  return routeRegex.test(currentPath);
};

/**
 * 从当前路径获取用户角色
 */
export const getUserRoleFromPath = (pathname: string): string | null => {
  if (pathname.startsWith("/admin")) return "admin";
  if (pathname.startsWith("/teacher")) return "teacher";
  if (pathname.startsWith("/student")) return "student";
  return null;
};

/**
 * 检查路径是否需要认证
 */
export const isProtectedRoute = (pathname: string): boolean => {
  const protectedPaths = ["/admin", "/teacher", "/student"];
  return protectedPaths.some((path) => pathname.startsWith(path));
};

/**
 * 生成面包屑导航数据
 */
export const generateBreadcrumbs = (
  pathname: string
): Array<{ title: string; path?: string }> => {
  const pathSegments = pathname.split("/").filter(Boolean);
  const breadcrumbs: Array<{ title: string; path?: string }> = [];

  let currentPath = "";

  for (const segment of pathSegments) {
    currentPath += `/${segment}`;

    // 根据路径段生成标题
    const title = getBreadcrumbTitle(segment, currentPath);
    if (title) {
      breadcrumbs.push({
        title,
        path: currentPath,
      });
    }
  }

  return breadcrumbs;
};

/**
 * 获取面包屑标题
 */
const getBreadcrumbTitle = (
  segment: string,
  fullPath: string
): string | null => {
  const titleMap: Record<string, string> = {
    admin: "管理后台",
    teacher: "教师工作台",
    student: "学生学习",
    home: "首页",
    dashboard: "仪表盘",
    profile: "个人资料",
    courses: "课程管理",
    teachers: "教师管理",
    students: "学生管理",
    discussions: "讨论管理",
    preparation: "备课设计",
    "class-management": "班级管理",
    "exam-generation": "考试生成",
    analysis: "学情分析",
    "learning-analysis": "学习分析",
    resources: "资源管理",
    exams: "考试中心",
    add: "新增",
  };

  return titleMap[segment] || null;
};
